let searchObj = new URLSearchParams(location.search);
console.log(searchObj);
let pid = Number(searchObj.get("id"));
let uid = localStorage.getItem("id");
console.log(pid);
window.onload = function () {
  let API = "http://jx.xuzhixiang.top/ap/api/detail.php";
  axios.get(API, { params: { id: pid } }).then((r) => {
    // console.log(r.data.data);
    let obj = r.data.data;

    let box1 = (document.querySelector(".gakki").innerHTML = `
   <img src="${obj.pimg}" alt="" />
 <button onclick=fn() class='btn'>加入购物车</button>
 <input type='number' min=1>
   `);
    let aaa = (document.querySelector(".aaa").innerHTML = `用户:${obj.uid}`);
    let name = (document.querySelector(".uname").innerHTML = `${obj.pname}`);
    //价格
    let bb = (document.querySelector(
      ".price"
    ).innerHTML = `京 东 价 <b>$${obj.pprice}</b>`);

    $(".gakki").hiZoom({
      width: 400,
      position: "right",
    });
  });
};

function fn() {
  let API = "http://jx.xuzhixiang.top/ap/api/add-product.php";
  let ipt = document.querySelector("input");
  pnum = ipt.value;
  if (pnum == 0) {
    alert("请选择加入购物车数量!");
  } else {
    axios.get(API, { params: { pnum, uid, pid } }).then((res) => {
      alert("加入购物车成功");
    });
  }
}

//点击购物车跳转
let btn1 = document.querySelector(".btn1");
btn1.onclick = function () {
  location.href = "购物车.html";
};

load();
function load() {
  let pagesize = 42;
  let pagenum = 0;
  let num = parseInt(Math.random() * 100);
  let API = "http://jx.xuzhixiang.top/ap/api/allproductlist.php";
  axios.get(API, { params: { pagenum: num, pagesize } }).then((res) => {
    // console.log(res.data.data);
    let str = "";
    let strUser = "";
    let Parr = res.data.data;
    Parr.forEach((v) => {
      str += `
      <a href="商品详情.html?id=${v.pid}"><li>
      <img src="${v.pimg}" alt="">
      <p>${v.pname}</p>
      <p>$${v.pprice}</p>
      <span>查看详情</span>
    </li></a>
      `;
    });
    let sp = (document.querySelector(".sp").innerHTML = str);
  });
}
